<template>
  <div class="searchBox">
    <el-form :inline="true" :model="searchData">
      <el-form-item class="labelstyle" label="用户名称">
        <el-input
          v-model="searchData.realname"
          placeholder="请输入用户名"
          clearable
        />
      </el-form-item>
      <el-form-item label="用户账号">
        <el-input
          v-model="searchData.userid"
          placeholder="请输入账号"
          clearable
        />
      </el-form-item>
      <el-form-item label="选择日期">
        <el-date-picker
          v-model="searchData.cdate"
          type="date"
          placeholder="选择日期"
          clearable
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dialogFormVisible = true">
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-dialog
        :close-on-click-modal="false"
        v-model="dialogFormVisible"
        title="新增用户"
      >
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleFormRef"
          status-icon
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" :label-width="formLabelWidth" prop="realname">
            <el-input class="input" v-model="ruleForm.realname" autocomplete="off"/>
          </el-form-item>

          <el-form-item label="用户ID" :label-width="formLabelWidth" prop="userid">
            <el-input class="input" v-model="ruleForm.userid" autocomplete="off" />
          </el-form-item>

          <el-form-item label="日期" :label-width="formLabelWidth" prop="cdate">
            <el-date-picker
              v-model="ruleForm.cdate"
              type="date"
              placeholder="选择日期"
              clearable
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button type="primary" @click="submitForm(ruleFormRef)"
              >确定</el-button
            >
            <el-button type="primary" @click="dialogFormVisible = false"
              >取消</el-button
            >
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">

//使用组件库后要注意样式冲突的问题

import { reactive, ref } from "vue";
import { dayjs } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import type { UsersListType, IAddUserFormType } from "@/typings/usersType";
import {addUserApi} from '@/api/usersApi'

interface IProps {
  data: UsersListType[];
  total: number;
  loadData: (page: number, search:Record<string, string>) => void;
  currentPage: number
}
const props = defineProps<IProps>();

const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const ruleFormRef = ref<FormInstance>();

const validateRealname = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("不能为空"));
  } else {
    callback();
  }
};

const validateUserid = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("不能为空"));
  } else {
    callback();
  }
};

const rules = reactive<FormRules<typeof ruleForm>>({
  realname: [{ validator: validateRealname, trigger: "blur" }],
  userid: [{ validator: validateUserid, trigger: "blur" }],
});

const searchData = reactive({
  realname: "",
  userid: "",
  cdate: "",
});

const ruleForm = reactive<any>({
  userid: "",
  realname: "",
  cdate: dayjs().format("YYYY-MM-DD"),
});

const onSearch = () => {
  props.loadData(props.currentPage,searchData)
//   let id =Number(searchData.userid);
//   let result = props.data.filter((item) => item.userid === id);
// //   console.log(searchData);
//   console.log(result);
  
};

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      let ret = await addUserApi(ruleForm)
        //1.请求
        //2.响应
        //3.关闭当前弹出层
        //4.请求一次
        console.log(ret);
    } else {
      console.log("提交失败");
      return false;
    }
  });
};
</script>

<style scoped>
.searchBox {
  padding-top: 20px;
  box-sizing: border-box;
  background-color: #ddd;
}
.input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
.labelstyle {
  margin-left: 30px;
}
</style>
